<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
    
         private $_id;
	public function authenticate()
	{
		$user= Usuario::model()->find("LOWER(nombre_usuario)=?", array(strtolower($this->username)));
                $cliente= Cliente::model()->find("LOWER(nombre_usuario)=?", array(strtolower($this->username)));
                $administrador= Administrador::model()->find("LOWER(nombre_usuario)=?", array(strtolower($this->username)));
                
        if ($user !== null)
        {
            if ($this->password === $user->contraseña){
                if($this->username === $user->nombre_usuario){
           
            $this->_id=$user->nombre_usuario;
            $this->setState("email", $user->email);
            $this->errorCode = self::ERROR_NONE;
                }else{
                    $this->errorCode = self::ERROR_USERNAME_INVALID;
                }
            }else{
                 $this->errorCode = self::ERROR_PASSWORD_INVALID;
            }
            
        }else if ($cliente !== null){
            
           if ($this->password === $cliente->contraseña){
                if($this->username === $cliente->nombre_usuario){
           
            $this->_id=$cliente->nombre_usuario;
            $this->setState("email", $cliente->email);
            $this->errorCode = self::ERROR_NONE;
                }else{
                    $this->errorCode = self::ERROR_USERNAME_INVALID;
                }
            }else{
                 $this->errorCode = self::ERROR_PASSWORD_INVALID;
            }
            
        }else if ($administrador !== null){
            
            if ($this->password === $administrador->contraseña){
                if($this->username === $administrador->nombre_usuario){
           
            $this->_id=$administrador->nombre_usuario;
            $this->setState("email", $administrador->email);
            $this->errorCode = self::ERROR_NONE;
                }else{
                    $this->errorCode = self::ERROR_USERNAME_INVALID;
                }
            }else{
                 $this->errorCode = self::ERROR_PASSWORD_INVALID;
            }
            
        }
        
        return !$this->errorCode;
	}
        
        public function getId(){
            return $this->_id;
        }
}